package com.example.demo.controller;


import com.example.demo.model.Result;
import com.example.demo.utils.OssUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/eduoss")
public class OssController {
	private OssUtil ossUtil;

	public OssController(OssUtil ossUtil) {
		this.ossUtil = ossUtil;
	}

	/**
	 * 图片上传调用OssUtil工具类的uploadFileAvatar方法
	 */
	@PostMapping("/fileOss")
	public Result UploadOssFile(MultipartFile avatar) {
		System.out.println(avatar);
		String url = ossUtil.uploadFileAvatar(avatar);
		System.out.println(url); //图片路径
		return Result.buildSuccess("success");
	}

	@PostMapping("/fileOss1")
	@ResponseBody
	public Result addImg(@RequestBody MultipartFile file, HttpServletRequest request, HttpServletResponse response)throws Exception {
		String url = ossUtil.uploadFileAvatar(file);
		System.out.println(url); //图片路径
		return Result.buildSuccess("success");
	}

//	@PostMapping("/fileOss2")
//	@ResponseBody
//	public Result addImg(@RequestParam("file") MultipartFile[] fileList,
//						 @RequestParam("name") String name,
//						 @RequestParam("content") String content)throws Exception {
//		System.out.println(fileList.length+"======");
//		System.out.println(name+"///");
//		System.out.println(content+"==***==");
//		if (fileList.length>0){
//			for (MultipartFile multipartFile : fileList) {
//				String url = ossUtil.uploadFileAvatar(multipartFile);
//				System.out.println(url);
//			}
//			return Result.buildSuccess("success");
//		}
//		return Result.buildFail("fail");
//	}



}
